package com.fira.web.controller.cms;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.fira.common.annotation.Log;
import com.fira.common.enums.BusinessType;
import com.fira.cms.domain.CmsBanner;
import com.fira.cms.service.ICmsBannerService;
import com.fira.common.core.controller.BaseController;
import com.fira.common.core.domain.AjaxResult;
import com.fira.common.utils.poi.ExcelUtil;
import com.fira.common.core.page.TableDataInfo;

/**
 * banner信息Controller
 * 
 * @author tony
 * @date 2021-10-09
 */
@Controller
@RequestMapping("/cms/banner")
public class CmsBannerController extends BaseController
{
    private String prefix = "cms/banner";

    @Autowired
    private ICmsBannerService cmsBannerService;

    @RequiresPermissions("cms:banner:view")
    @GetMapping()
    public String banner()
    {
        return prefix + "/banner";
    }

    /**
     * 查询banner信息列表
     */
    @RequiresPermissions("cms:banner:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CmsBanner cmsBanner)
    {
        startPage();
        List<CmsBanner> list = cmsBannerService.selectCmsBannerList(cmsBanner);
        return getDataTable(list);
    }

    /**
     * 导出banner信息列表
     */
    @RequiresPermissions("cms:banner:export")
    @Log(title = "banner信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CmsBanner cmsBanner)
    {
        List<CmsBanner> list = cmsBannerService.selectCmsBannerList(cmsBanner);
        ExcelUtil<CmsBanner> util = new ExcelUtil<CmsBanner>(CmsBanner.class);
        return util.exportExcel(list, "banner");
    }

    /**
     * 新增banner信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存banner信息
     */
    @RequiresPermissions("cms:banner:add")
    @Log(title = "banner信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CmsBanner cmsBanner)
    {
        return toAjax(cmsBannerService.insertCmsBanner(cmsBanner));
    }

    /**
     * 修改banner信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        CmsBanner cmsBanner = cmsBannerService.selectCmsBannerById(id);
        mmap.put("cmsBanner", cmsBanner);
        return prefix + "/edit";
    }

    /**
     * 修改保存banner信息
     */
    @RequiresPermissions("cms:banner:edit")
    @Log(title = "banner信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CmsBanner cmsBanner)
    {
        return toAjax(cmsBannerService.updateCmsBanner(cmsBanner));
    }

    /**
     * 删除banner信息
     */
    @RequiresPermissions("cms:banner:remove")
    @Log(title = "banner信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cmsBannerService.deleteCmsBannerByIds(ids));
    }
}
